﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.Utils.Frames;
using DevExpress.XtraEditors;
using XCI.Core;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmStandardLogin : BaseForm, ILoginForm
    {
        private Font drawFont;
        private SolidBrush solidBrush;

        public FrmStandardLogin()
        {
            InitializeComponent();
        }

        public Form OwnForm => this;
        public Control AccountEdit => editAccount;
        public Control PasswordEdit => editPassword;
        public Control LoginButton => btnLogin;
        public CheckEdit RememberPasswordEdit => editRememberPassword;
        public CheckEdit AutoLoginEdit => editAutoLogin;
        public LoadingMask LoadingPanel => Loading;

        private void InitForm()
        {
            drawFont = new Font(AppearanceObject.DefaultFont.Name, AppearanceObject.DefaultFont.Size + 6, FontStyle.Bold);
            var color = Color.White;
            if (FrameHelper.IsDarkSkin(GetActiveLookAndFeel())) color = Color.Silver;

            solidBrush = new SolidBrush(color);
            LoginFormManager.LoadConfig(this);
            NativeMethods.SetForegroundWindow(this.Handle);
        }

        private void StandardLoginForm_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            await LoginFormManager.LoginAsync(this);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Startup.Exit();
        }

        private void editSavePwd_CheckedChanged(object sender, EventArgs e)
        {
            if (!editRememberPassword.Checked)
            {
                editAutoLogin.Checked = false;
            }
        }

        private void editAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (editAutoLogin.Checked)
            {
                editRememberPassword.Checked = true;
            }
        }

        private void editLoginName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && editAccount.Text.Trim().Length > 0)
            {
                editPassword.Select();
                e.Handled = true;
            }
        }

        private void editPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnLogin.PerformClick();
                e.Handled = true;
            }
            else if (e.KeyChar == 27)
            {
                editAccount.Focus();
                editAccount.SelectionStart = editAccount.Text.Length;
                e.Handled = true;
            }
        }

        private void PicHeader_PaintEx(object sender, DevExpress.XtraGrid.PaintExEventArgs e)
        {
            var title = App.Setting.Name;
            if (string.IsNullOrEmpty(title)) return;

            var size = e.Cache.Graphics.MeasureString(title, drawFont);
            var x = (picHeader.Width - size.Width) / 2f;
            var y = (picHeader.Height - size.Height) / 2f;
            e.Cache.Graphics.DrawString(title, drawFont, solidBrush, x, y);
        }

        private void BtnReg_Click(object sender, EventArgs e)
        {
            MessageBoxHelper.ShowMessage("正在开发");
        }

        private void BtnFindPwd_Click(object sender, EventArgs e)
        {
            MessageBoxHelper.ShowMessage("正在开发");
        }
    }
}